﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Restaurant.BUS;
using System.Windows.Forms;

namespace Restaurant.DAO
{
    public class MonAnDB
    {
        private SqlConnection conn = KetNoiDB.TaoKetNoi();

        public DataTable LoadMonAn()
        {
            conn.Open();
            string strCmd = "dbo.spLoadMonAn";
            SqlCommand cmd = new SqlCommand(strCmd, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtab = new DataTable();
            adapt.Fill(dbtab);
            conn.Close();
            return dbtab;
        }

        public bool XoaMonAn(int ma)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spXoaMonAn";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                //return false;
                MessageBox.Show(ex.ToString());
                return false;
            }
        }

        public DataTable LoadLoaiMonAn()
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spLoadLoaiMonAn";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return dbtb;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayMaLoaiTheoTen(string ten)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spLayMaLoaiTheoTen";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ten", ten));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return dbtb;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool ThemMonAn(string ten, int dongia, int maloai,ref int mamonan)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spThemMonAn";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ten", ten));
                cmd.Parameters.Add(new SqlParameter("@dongia", dongia));
                cmd.Parameters.Add(new SqlParameter("@maloai", maloai));
                cmd.Parameters.Add(new SqlParameter("@ma", mamonan));
                cmd.Parameters["@ma"].Direction = ParameterDirection.Output;
                
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                mamonan = int.Parse(cmd.Parameters["@ma"].Value.ToString());
                conn.Close();
                
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }

        public bool ThemCTMonAn(int mamon, int manl,int soluong)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spThemCTMonAn";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@mamon", mamon));
                cmd.Parameters.Add(new SqlParameter("@manl", manl));
                cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool XoaCTMonAn(int mamon)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spXoaCTMonAn";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@mamon", mamon));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        public DataTable TimMonAn(int ma)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spTimMonAn";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayTenLoaiTheoMa(int ma)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spLayTenLoaiTheoMa";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return dbtb;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayCTMonAn(int ma)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spLayCTMonAn";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool SuaMonAn(string ten, int dongia, int maloai, int mamonan)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spSuaMonAn";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ten", ten));
                cmd.Parameters.Add(new SqlParameter("@dongia", dongia));
                cmd.Parameters.Add(new SqlParameter("@maloai", maloai));
                cmd.Parameters.Add(new SqlParameter("@ma", mamonan));

                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SuaCTMonAn(int mamon, int manl, int soluong)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spSuaCTMonAn";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@mamon", mamon));
                cmd.Parameters.Add(new SqlParameter("@manl", manl));
                cmd.Parameters.Add(new SqlParameter("@soluong", soluong));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
